package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 分页遍历/模糊查询角色信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list=roleService.findAll();
            System.out.println(list);
            return new Result(true,"查询角色成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    /**
     * 根据id查询角色信息
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role=roleService.findById(id);
            return new Result(true,"查询角色成功",role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/add.do")
    public Result add(Integer[] perIds,Integer[] menuIds,@RequestBody Role role){
        try{
            roleService.add(role,perIds,menuIds);
            return new Result(true,"新增角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
    }

    /**
     * 编辑用户信息
     * @param perIds
     * @param menuIds
     * @param role
     * @return
     */
    @RequestMapping("/update.do")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result update(Integer[] perIds,Integer[] menuIds,@RequestBody Role role){
        try{
            roleService.update(role, perIds,menuIds);
            return new Result(true,"编辑角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑角色失败");
        }
    }

    /**
     * 依据id删除指定角色
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id){
            return roleService.delete(id);
    }
}

